package com.wangyg.juc.Semaphore.exclude;

import java.util.concurrent.Semaphore;

/**
 * Created by wangyg6 on 2018/7/19.
 */
public class PrinterTest {
	private static final Semaphore semaphore = new Semaphore(1);//1个的时候相当于互斥锁

	public static void print(String msg) throws InterruptedException {
		try {
			semaphore.acquire();
			long time = (long) (Math.random()*1000);
			System.out.println(Thread.currentThread().getName() + "--开始打印:" + msg);
			Thread.sleep(time);
			System.out.println(Thread.currentThread().getName() + "耗时" + time + "ms");
		} finally {
			semaphore.release();
		}
	}
}
